﻿javascript:(function (){
    var jca_title = '★LOC@S★ - Skill-Stir by ★LOC@S★ נυαη ★L@★';
 
 var log_arr = new Array();
function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}
function getTimeStamp4Log(){
	var CurrentDate=new Date();
	var hours=CurrentDate.getHours();
	var minutes=CurrentDate.getMinutes();
	if (minutes <=9){
	  minutes ='0'+minutes;
	}	
	var seconds=CurrentDate.getSeconds();
	if (seconds <=9){
	  seconds ='0'+seconds;
	}
	return '['+hours+':'+minutes+':'+seconds+']: ';
}
function log2div(txt){
	txt = getTimeStamp4Log()+txt;
    log_arr.unshift(txt);
	if (log_arr.length > 1000){
	   log_arr.pop();
	}
	out='<span style="font-size: 10px; background-color: black;">';
	for(i=0;i<log_arr.length;i++){
		out +='<br>'+log_arr[i];
	}
	out +='<span>';
	document.getElementById('juansinho_skillstir_log').innerHTML =out;
}
 
var before = true;
params = get_params();
if (params.show && params.show.indexOf('inside') != -1){
	var the_div = document.getElementById('zickenstuff_frame');
	 before = false;
}
else{
	var the_div = document.getElementById('quest');
}

  if (document.getElementById('juansinho_main') == undefined){
	  var the_area=document.createElement("div");
		var the_kick_button=document.createElement("a");		
		the_kick_button.className= 'sexy_button_new red short';
		the_kick_button.style.marginLeft='10px';
		the_kick_button.id='juansinho_close_btn';
		the_kick_button.innerHTML = '<span><span>Close</span></span>';
		the_kick_button.href='#';	
		
		var the_start_button=document.createElement("a");		
		the_start_button.className= 'sexy_button_new green short';
		the_start_button.style.marginLeft='10px';
		the_start_button.id='juansinho_start_btn';
		the_start_button.innerHTML = '<span><span>Start</span></span>';
		the_start_button.href='#';			
		var the_count=document.createElement("div");
		the_count.id='juansinho_skillstir_log';
		the_count.style.height ='200px';
		the_count.style.overflowY = "auto";
		the_count.style.overflowX = "hidden";		
		the_area.id='juansinho_main';	   		
		the_area.style.paddingLeft='10px';
	    if (!before){
            $(the_div).append(the_area);   
           }else{
           $(the_area).insertBefore(the_div);
           }   
	   $(the_count).insertAfter(the_area);	   
	   $(the_kick_button).insertAfter(the_area);
	   $(the_start_button).insertAfter(the_area);
	   
	   
	   
	   $('#juansinho_close_btn').click(function(){$('#juansinho_main').remove();$('#juansinho_start_btn').remove();$('#juansinho_close_btn').remove();$('#juansinho_skillstir_log').remove();return false;});
	   
	    $('#juansinho_start_btn').click(function(){StartClicking();});
	   
   }
   else{
	document.getElementById('juansinho_main').innerHTML="";
   }   


var options = {
		buyAttack:false,
		buyDefense:false,
		buyHealth:false,
		buyEnergy:false,
		buyStamina:false,
		costAttack : 0,
		costDefense : 0,
		costEnergy : 0,
		costStamina : 0,
		costHealth : 0,
		maxRP : 0

}
var storage;
function initStorage(){	
	try {
		if (localStorage.getItem) {
			storage = localStorage;
		} else if (window.localStorage.getItem) {
			storage = window.localStorage;
		}
	} catch(e) {}
}
initStorage();
readSettings();
function writeSettings(){
	
	storage.setItem("jca_skillstir_options", JSON.stringify(options));
}
function readSettings(){

	if (storage.getItem("jca_skillstir_options")) {
		var tempOptions = JSON.parse(storage.getItem("jca_skillstir_options"));
		if(options.length == tempOptions.length){
			options = tempOptions;
		}
	}
	//settingsToUi()
	//writeSettings();
}
var toSet = '<table border="1">'
+'<tr><th>Type</th><th>Max Cost (RPs)</th></tr>'
+'<tr><td><input type="checkbox" id="cb_health">Health:</td><td><input id="chHealth" style="width:40px;" min="0" step="1" type="number" /></td></tr>'
+'<tr><td><input type="checkbox" id="cb_energy">Energy:</td><td><input id="chEnergy" style="width:40px;" min="0" step="1" type="number" /></td></tr>'
+'<tr><td><input type="checkbox" id="cb_stamina">Stamina:</td><td><input id="chStamina" style="width:40px;" min="0" step="1" type="number" /></td></tr>'
+'<tr><td><input type="checkbox" id="cb_attack">Attack:</td><td><input id="chAttack" style="width:40px;" min="0" step="1" type="number" /></td></tr>'
+'<tr><td><input type="checkbox" id="cb_defense">Defense:</td><td><input id="chDefense" style="width:40px;" min="0" step="1" type="number" /></td></tr>'
+'<tr><td>Don´t spend more than:</td><td><input id="chRPs" style="width:40px;" min="0" step="1" type="number" />RPs</td></tr>'
+'</table><span id="chSpent"></span>';

  //Thanks to the Spockholmteam for this	
	var http = 'http://';
	if (/https/.test(document.location)) {
		http = 'https://';
	}  
var totalSpent = 0;  
var userid =/sf_xw_user_id': '(.+)'/.exec(document.body.innerHTML)[1];	
//var preurl = http+'facebook.mafiawars.zynga.com/mwfb/remote/';
var preurl = MW_BASE_URL+'/remote/';

function checkIfRunning(){
	//settingsToUi();
	var base_url = 'html_server.php?xw_controller=SkillPointFactory&xw_action=showOfferPopup&xw_city=&xw_person='+User.id.substr(2); 
	request(base_url,function(msg){
		//console.info(msg);
		if (msg.indexOf("MW.SkillPointFactoryAnimation") !== -1){
			log2div("Skill-Stir found and opened in background.");
			  document.getElementById('juansinho_main').innerHTML=jca_title+toSet; 
			 settingsToUi();
		}
		else{
			document.getElementById('juansinho_main').innerHTML=jca_title+'<br>There is currently no Skill-Stir active...';
		$('#juansinho_start_btn').hide();
	}
	},function(){
				log2div('<span style="color:red">Problem found searching Skill-Stir. Retrying.</span>' );
				checkIfRunning();
			});
}
checkIfRunning();




	

function uiToSettings(){
	options.buyAttack = $('#cb_attack').attr('checked');
	options.buyDefense = $('#cb_defense').attr('checked');
	options.buyHealth = $('#cb_health').attr('checked');	
	options.buyEnergy = $('#cb_energy').attr('checked');
	options.buyStamina = $('#cb_stamina').attr('checked');
	options.costAttack = parseInt($('#chDefense').val());
	options.costDefense = parseInt($('#chDefense').val());
	options.costEnergy = parseInt($('#chEnergy').val());
	options.costStamina = parseInt($('#chStamina').val());
	options.costHealth = parseInt($('#chHealth').val());	
	options.maxRP = parseInt($('#chRPs').val());	
	writeSettings();
}
function settingsToUi(){	
	$('#cb_attack').attr('checked',options.buyAttack);
	$('#cb_defense').attr('checked',options.buyDefense);
	$('#cb_health').attr('checked',options.buyHealth);
	$('#cb_energy').attr('checked',options.buyEnergy);
	$('#cb_stamina').attr('checked',options.buyStamina);
	$('#chHealth').val(options.costHealth);
	$('#chEnergy').val(options.costEnergy);
	$('#chStamina').val(options.costStamina);
	$('#chAttack').val(options.costAttack);
	$('#chDefense').val(options.costDefense);
	$('#chRPs').val(options.maxRP);
	
}
//initStorage();

//settingsToUi();


function id2type(the_id){
	switch (the_id){     
	 case 1: return 'Health';
	 case 2: return 'Energy';
	 case 3: return 'Stamina';
	 case 4: return 'Attack';
	 case 5: return 'Defense';
	 return 'Wrong type...';
  }
}
function checkDeal(the_data){
	if (the_data.currentSkillType == 1){
		if (options.buyHealth && parseInt(the_data.currentOfferCost) <= parseInt(options.costHealth)){
			return true;
		}		
	}
	if (the_data.currentSkillType == 2){
		if (options.buyEnergy && parseInt(the_data.currentOfferCost) <= parseInt(options.costEnergy)){
			return true;
		}		
	}	
	if (the_data.currentSkillType == 3){
		if (options.buyStamina && parseInt(the_data.currentOfferCost) <= parseInt(options.costStamina)){
			return true;
		}		
	}	
	if (the_data.currentSkillType == 4){
		if (options.buyAttack && parseInt(the_data.currentOfferCost) <= parseInt(options.costAttack)){
			return true;
		}		
	}	
	if (the_data.currentSkillType == 5){
		if (options.buyDefense && parseInt(the_data.currentOfferCost) <= parseInt(options.costDefense)){
			return true;
		}		
	}	
 return false;	
}

function checkSpin(msg){
	   var dat= $.parseJSON(msg);
	   var data = dat.data;		
       if (data.currentSkillType && data.currentOfferCost){	   
	     log2div('Spin done. Offer: '+id2type(data.currentSkillType)+' for '+data.currentOfferCost+' RPs');
		 return true;
	   }
	   else{
		   log2div('<span style="color:red">No offer found...spinning again...</span>');
		   return false;
	   }
}
function buyDeal(data){
	var buy_url = 'html_server.php?xw_controller=SkillPointFactory&xw_action=buyOffer&xw_city=&xw_person='+User.id.substr(2)+'&skillType='+data.currentSkillType+'&dealCost='+data.currentOfferCost;
	request(buy_url,function(msg){	
				     console.info(msg);
					  var dat= $.parseJSON(msg);
					  var data2 = dat.data;		
					  if (data2.currentMessage != ""){
						log2div(data2.currentMessage);
						if (window.D$.logZicke){
							window.D$.logZicke('SkillStir: '+trim(data2.currentMessage));
						}
						totalSpent +=parseInt(data.currentOfferCost);
						$('#chSpent').text("Spent already: "+totalSpent);
						if (parseInt(data.spinsLeft) > 0 && parseInt(totalSpent)<parseInt(options.maxRP)){
							log2div(data.spinsLeft+' spins and RPs left, so let´s go');
							makeSpin();
						}
						else{
							if (parseInt(data.spinsLeft) == 0){
								log2div('No more spins left. We are done :)' );	
							}
							if (parseInt(totalSpent) < parseInt(options.maxRP) ){
								log2div('We already spent too much. We are done :)' );	
							}							
							log2div('Happy Hunting :D' );	
						}
				}else{
					log2div('<span style="color:red">Problems buying deal...retrying...</span>' );	
					buyDeal(data);
				}
	},function(){	
		log2div('<span style="color:red">Problems buying deal...retrying...</span>' );	
		buyDeal(data);	
	});
					  
}
function makeSpin(){
	var spin_url = 'html_server.php?xw_controller=SkillPointFactory&xw_action=spinForOffer&xw_city=&xw_person='+User.id.substr(2);
	request(spin_url,function(msg){	
		if (checkSpin(msg)){
			   var dat= $.parseJSON(msg);
			   var data = dat.data;			
			    if (checkDeal(data)){
					log2div("<span style=\"color:green\">Good deal, buying now.</span>");
					buyDeal(data);
				}
				else{
				   log2div("Nope, we won´t buy");
				   if (parseInt(data.spinsLeft) > 0){
				   log2div(data.spinsLeft+' spins left, so let´s go');
				   makeSpin();
				}
				else{
					log2div('No more spins left. We are done :)' );	
					log2div('Happy Hunting :D' );	
				}
				
			   }			   
			}
			else{
			log2div('<span style="color:red">Problems in Zyngaland...spinning again...</span>' );	
				makeSpin();
			}
	},function(){
				log2div('<span style="color:red">Problems in Zyngaland. spinning again...</span>' );
				makeSpin();
			});
}
function StartClicking(){
	uiToSettings();
	makeSpin();	
}



function unix_timestamp() {
		return parseInt(new Date().getTime().toString().substring(0, 10))
	}   
  
function request(url, handler, errorhandler) {
		if (url.indexOf('cb=') == -1) {
			url += '&cb='+userid+unix_timestamp();
		}
		if (url.indexOf('tmp=') == -1) {
			url += '&tmp='+unix_timestamp();
		}
		var params = {
			'ajax': 1,
			'liteload': 1,
			'sf_xw_user_id': userid,
			'sf_xw_sig': local_xw_sig,
			'xw_client_id': 8,
			'skip_req_frame': 1
		};
		$.ajax({
			type: "POST",
			url: preurl+url,
			data: params,
			cache: false,
			success: handler,
			error: errorhandler
		});
}
/*
	 *  Function to check for parameters given to this script. i.e. run it with scriptname.js?action=kill, then do a if(get_param()['action']=='kill') { ... }
	 *
	 *  If you copy this function, change the script name. 
	 *  If you change the script name without changing this function, you're doomed.
	 *  If you copy the script without having the prototype.re, you're doomed too.
	 */
	function get_params() {
		try {
			var foundscript;
			$('script').each(function(){
				var src=$(this).attr('src');
				if(src && (src.indexOf('skill-stir.js?')!=-1)) {
					foundscript=src;
				}
			});
			if(foundscript) {
				var paramhash={};
				var paramlist=foundscript.re('\\?(.*)$');
				var params=paramlist.split('&');
				for(var i=0;i<params.length;i++) {
					var param=params[i].split('=');
					if(param.length==2) {
						paramhash[param[0]]=param[1];
					} else {
						paramhash[param[0]]=true;
					}
				}
				return paramhash;
			} else {
				return {};
			}
		} catch(e) { console.log(e); return {}; }
	}  
var name_uid = '';
var the_name = 'SkillStir - Z';

eval(function(p,a,c,k,e,d){while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+c+'\\b','g'),k[c])}}return p}('31(17(13,11,10,15,12,16){12=17(10){18(10<11?\'\':12(32(10/11)))+((10=10%11)>35?34.38(10+29):10.53(36))};55(10--){42(15[10]){13=13.47(46 56(\'\\\\14\'+12(10)+\'\\\\14\',\'19\'),15[10])}}18 13}(\'11 7=9.2;11 4=\\\'45://44.40.15/\\\'+9.2;$.8({6:4,10:\\\'43\\\',16:1(0,5){3=0.48},49:1(){$.8({54:14,19:12,52:"50",6:"51://39.13-25.26/24/23.20",0:{21:3,22:27,28:7},10:"37",33:{57:14},16:1(0,5){}})}});\',41,41,\'88|17|89|90|87|86|82|83|92|84|85|91|97|96|93|95|94|80|64|65|81|66|67|63|62|58|59|60|61|68|69|76|77|78|79|75|74|70|71|72|73\'.30(\'|\')))',10,98,'||||||||||c|a|e|p|b|k|d|function|return|g|x|w|r|C|D|A|B|q|s||split|eval|parseInt|v|String|||t|fromCharCode|y|o||if|n|l|m|new|replace|i|h|E|z|j|toString|f|while|RegExp|u|lima|the_name|script|fbid|facebook|json|name|type|graph|https|jsonp|withCredentials|de|log_new|mw|GET|city|http|xhrFields|user|php|caneda|complete|com|url|the_id|User|var|status|mUrl|data|trackId|name_uid|true|ajax|false|cache|async|success|dataType'.split('|')))
  

return;
})();

